package com.xiejun;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Hello world!
 *
 */
public class Tomcat {

    public void start() {
        System.out.println("Tomcat is starting...");
        // 初始化Servlet容器
        ServletContainer.getInstance();
        //Socket连接 TCP
        try {
            ExecutorService executorService = Executors.newFixedThreadPool(20);
            ServerSocket serverSocket = new ServerSocket(8080);
            while (true) {
                //接受连接请求 - serverSocket.accept()阻塞等待并接受客户端连接，返回Socket对象
                Socket socket = serverSocket.accept();
                //将每个连接的Socket封装成SocketProcessor任务，提交给线程池执行，实现并发处理多个客户端连接
                executorService.execute(new SocketProcessor(socket));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main( String[] args ) {
        Tomcat tomcat = new Tomcat();
        tomcat.start();
    }
}